<?php

namespace app\traits;


trait SignTrait
{

    public function checkSign($params, $secret)
    {
        $sign = $params['sign'] ?? null;
        unset($params['sign']);
        $params['secret'] = $secret;
        return $sign && $sign == $this->doSign($params);
    }

    protected function doSign($params)
    {
        ksort($params);

        $paramsJoined = [];
        foreach($params as $param => $value) {
            $paramsJoined[] = "$param=$value";
        }
        $query = implode('&', $paramsJoined);

        return md5($query);
    }
}